package com.wf.it.hpweb;

import com.wf.it.common.api.DeptService;
import com.wf.it.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Api("DeptController-部门管理API")
public class DeptController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DeptService service;

    @ApiOperation("获取所有的部门信息")
    @GetMapping("/dept/all")
    public Result showAll(){
        Result result= (Result) redisTemplate.opsForValue().get("dept_all");
        if(result!=null) {
            System.out.println("从缓存中获取dept_all,直接返回");
            return result;
        }
        result =service.findAll();
        if(result.getState()!=200)
            return result;
        redisTemplate.opsForValue().set("dept_all",result,1, TimeUnit.DAYS);
        System.out.println("从数据库获取dept_all");
        return result;
    }
    @ApiOperation("根据id获取单个部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="要展示的部门id",paramType = "path")
    })
    @GetMapping("/dept/{id}")
    public Result showDetail(@PathVariable("id") int deptid){
       return  service.findById(deptid);
    }

}
